﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RenWuZhi.Models;
using System.IO;
using System.Dynamic;

namespace RenWuZhi.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "人物志——记录平凡人的一生！";
            RenWuZhiDBEntities dbModel = new RenWuZhiDBEntities();
            dynamic file = GetFiler();

            ViewBag.file = file;
            string name = Path.GetFileNameWithoutExtension(file.filerName);
            var model = (from entityList in dbModel.rwz_PeopleContent
                         orderby entityList.PC_UpdateDate descending, entityList.PC_CreateDate descending
                         where entityList.PC_Title.IndexOf(name) > 0 || entityList.PC_Content.IndexOf(name) > 0
                         select entityList).Take(5);
            return View(model);
        }

        //
        // POST: /PeopleContent/Edit/5
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            RenWuZhiDBEntities dbModel = new RenWuZhiDBEntities();
            string text = collection["text1"].Trim();
            if (text == string.Empty)
                return Index();
            //var data = from entityList in dbModel.rwz_PeopleContent.ToList()
            //                 orderby entityList.PC_UpdateDate descending, entityList.PC_CreateDate descending
            //                 where entityList.PC_Content.IndexOf(text) > 0 || entityList.PC_Title.IndexOf(text) > 0
            //                 select entityList;
            //ViewData.Model = data.ToList<rwz_PeopleContent>();
            return RedirectToAction("Index", "PeopleContent", new { id = text });
            //return View();
        }

        //获取最新创建的图型名称
        private dynamic GetFiler()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"Content\Files\SysFiles\";
            dynamic filer = new ExpandoObject();
            string[] dirs = Directory.GetFiles(path);
            var filers = from a in dirs
                         orderby Directory.GetCreationTime(a)
                         select a;
            filer.path = filers.First();

            string filerName = Path.GetFileName(filer.path);
            filer.filerName = filerName;
            return filer;
        }

        private List<rwz_PeopleContent> data(string filerName)
        {
            //获取数据库信息
            Models.RenWuZhiDBEntities db = new RenWuZhiDBEntities();
            List<rwz_PeopleContent> data = db.rwz_PeopleContent.Where(m => m.PC_Content.StartsWith(filerName)).Take(4).ToList<rwz_PeopleContent>();

            return data;
        }



        public ActionResult About()
        {
            return View();
        }
    }
}
